package com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.group_details.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.GroupDetailsBean;
import com.wangjiyuan.giftshuo.config.classify.ClassifyConstant;
import com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.group_details.GroupDetWebView;
import com.wangjiyuan.giftshuo.utils.RecyclerViewAdapterHelper;

import java.io.Serializable;
import java.util.List;

/**
 * Created by QXL on 2016/11/22.
 */

public class GroupDetRecyclerAdapter extends RecyclerViewAdapterHelper<GroupDetailsBean.DataBean.ItemsBean> {

    public GroupDetRecyclerAdapter(Context context, List<GroupDetailsBean.DataBean.ItemsBean> list) {
        super(context, list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_essay, parent, false);
        return new ViewHolderGroupDet(view);
    }

    @Override
    public void onBindMyViewHolder(final RecyclerView.ViewHolder holder, int position) {

        GroupDetailsBean.DataBean.ItemsBean itemsBean = mList.get(position);
//        最上面分割线不可见
        if(position==0){
            ((ViewHolderGroupDet)holder).view.setVisibility(View.GONE);
        }
        Glide.with(mContext)
                .load(itemsBean.getAuthor().getAvatarUrl())
                .asBitmap()
                .placeholder(mContext.getResources().getDrawable(R.mipmap.post_place_holder_image))
                .centerCrop()
                .into(new BitmapImageViewTarget(((ViewHolderGroupDet)holder).avatar) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        ((ViewHolderGroupDet)holder).avatar.setImageDrawable(circularBitmapDrawable);
                    }
                });
        ((ViewHolderGroupDet) holder).nickname.setText(itemsBean.getAuthor().getNickname()+"");
        ((ViewHolderGroupDet) holder).introduction.setText(itemsBean.getAuthor().getIntroduction()+"");

        Glide.with(mContext)
                .load(itemsBean.getCoverImageUrl())
                .placeholder(mContext.getResources().getDrawable(R.mipmap.post_place_holder_image))
                .centerCrop()
                .into(((ViewHolderGroupDet)holder).cover_image);

        ((ViewHolderGroupDet) holder).title.setText(itemsBean.getTitle()+"");
        ((ViewHolderGroupDet) holder).description.setText(itemsBean.getShareMsg()+"");

        ((ViewHolderGroupDet) holder).column.setText(itemsBean.getColumn().getTitle()+"");
        ((ViewHolderGroupDet) holder).like_count.setText(itemsBean.getLikesCount()+"");
    }

    class ViewHolderGroupDet extends RecyclerView.ViewHolder implements View.OnClickListener{
        View view;//分割线
        ImageView avatar;
        TextView nickname;
        TextView introduction;
        LinearLayout linearLayoutWeb;
        ImageView cover_image;
        TextView title;
        TextView description;
        TextView column;
        TextView like_count;
        LinearLayout essay_item;
        public ViewHolderGroupDet(View itemView) {
            super(itemView);
            view=itemView.findViewById(R.id.essay_cut_line);
            avatar = (ImageView) itemView.findViewById(R.id.avatar);
            nickname = (TextView) itemView.findViewById(R.id.nickname);
            introduction = (TextView) itemView.findViewById(R.id.introduction);
            cover_image = (ImageView) itemView.findViewById(R.id.cover_image);
            int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;
            ViewGroup.LayoutParams layoutParams = cover_image.getLayoutParams();
            layoutParams.width = widthPixels;
            layoutParams.height = layoutParams.width * 396 / 720;

            title = (TextView) itemView.findViewById(R.id.title);
            description = (TextView) itemView.findViewById(R.id.description);
            column = (TextView) itemView.findViewById(R.id.column);
            like_count = (TextView) itemView.findViewById(R.id.like_count);
            essay_item = (LinearLayout) itemView.findViewById(R.id.essay_item);

            linearLayoutWeb= (LinearLayout) itemView.findViewById(R.id.linearLayout_webView);
            linearLayoutWeb.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent intent=new Intent(mContext, GroupDetWebView.class);
            Bundle bundle=new Bundle();
            switch (v.getId()){
                case R.id.linearLayout_webView:
                    intent.putExtra(ClassifyConstant.LIST_POSITION,getAdapterPosition());
                    Log.e("webView","check mPosition_ID==="+getAdapterPosition());
                    bundle.putSerializable(ClassifyConstant.LIST_KEY, (Serializable) mList);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                    break;
            }
        }
    }
}
